﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.User;

public partial class board_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.BoardId] != null)
        {
            Response.Redirect("answer-question.aspx");
        }

        if (!IsPostBack)
        {
        }
        else
        {
            lblLoginMessage.Text = "";
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        #region Existing Customer
        if (Page.IsValid)
        {
            AutoLogin(); //100
        }
        #endregion
    }

    private void AutoLogin()
    {
        AccountDB _accountdb = new AccountDB();
        Account _account = _accountdb.GetUserAccount(txtExEmail.Text.ToLower());

        if (_account != null)
        {
            if (_account.UserTypeId == 100)
            {
                if (Server.HtmlDecode(txtExAuthCode.Text) == _account.AuthCode)
                {
                    if (_account.Status == true)
                    {
                        if (_account.Info1 == "0")
                        {
                            //Reset Email Fail Message
                            _account.Info1 = "";
                            _account.Info2 = "";
                            _accountdb.Insert(_account);
                        }

                        divExErrorMessage.Visible = false;
                        Session[CE.Global.General.BoardId] = _account.UID;
                        _account = null;
                        Response.Redirect("answer-question.aspx");
                    }
                    else
                    {
                        divExErrorMessage.Visible = true;
                        lblLoginMessage.Text = "Please verify your account by carefully following the instructions in the email. ";
                        lblLoginMessage.Text += "<br>Click <a href='resend-user-activate.aspx?email=" + _account.Email + "'>here</a> to resend the confirmation email.";
                    }
                }
                else
                {
                    divExErrorMessage.Visible = true;
                    lblLoginMessage.Text = "Email address or password is incorrect.";
                }
            }
            else
            {
                divExErrorMessage.Visible = true;
                lblLoginMessage.Text = "You are not admin/support.";
            }
        }
        else
        {
            divExErrorMessage.Visible = true;
            lblLoginMessage.Text = "Email address or password is incorrect.";
        }

        lblLoginMessage.ForeColor = System.Drawing.Color.Red;
    }

}

